﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KhoanTienNhapTruongService;
namespace eTMS.Controllers{
public class KhoanTienNhapTruongControllerBase : CommonController{
	public KhoanTienNhapTruongControllerBase()
		: base()
	{	
		this.dao = new KhoanTienNhapTruongServiceClient();
	}
	~KhoanTienNhapTruongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KhoanTienNhapTruongEntity oKhoanTienNhapTruong;	
	public KhoanTienNhapTruongEntity KhoanTienNhapTruongObject{
		get{
			return oKhoanTienNhapTruong;
		}
		set{
			oKhoanTienNhapTruong=value;
		}
	}
	protected KhoanTienNhapTruongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKhoanTienNhapTruong(false);			
			return _Table;
		}
	}
	public void LoadAllDSKhoanTienNhapTruong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKhoanTienNhapTruong==null)return false;
		bool result=true;
		try{
			oKhoanTienNhapTruong = dao.SaveRet(oKhoanTienNhapTruong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKhoanTienNhapTruong.Add(oKhoanTienNhapTruong);
		}
		return result;
	}
	public bool Update(){
		if(oKhoanTienNhapTruong==null)return false;
		return dao.Update(oKhoanTienNhapTruong);
	}
	public bool Delete(){
		if(oKhoanTienNhapTruong==null)return false;
		bool retvalue = dao.Delete(oKhoanTienNhapTruong);
		if(retvalue){			
			_ListKhoanTienNhapTruong.Remove(oKhoanTienNhapTruong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 KhoanTienNhapTruong_ID){
		bool retvalue = dao.DeleteByID(KhoanTienNhapTruong_ID);		
		if(retvalue){
			KhoanTienNhapTruongEntity oDeleted=FindKhoanTienNhapTruong(KhoanTienNhapTruong_ID);
			if(oDeleted!=null){
				_ListKhoanTienNhapTruong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKhoanTienNhapTruong = new KhoanTienNhapTruongEntity();
	}
    //Copy data from data row in grid to object
    public KhoanTienNhapTruongEntity MapDataRowToObject(DataRow row){
		KhoanTienNhapTruongEntity o = new KhoanTienNhapTruongEntity();
		 if(!row.IsNull("SoTien"))
			o.SoTien=ConvertNumber(row["SoTien"]);
		 if(!row.IsNull("KhoanTien_ID"))
			o.KhoanTien_ID=(Int32)(row["KhoanTien_ID"]);
		 if(!row.IsNull("KhoanTienNhapTruong_ID"))
		    o.KhoanTienNhapTruong_ID=(Int32)(row["KhoanTienNhapTruong_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KhoanTienNhapTruongEntity o,DataRow row){
		row["SoTien"]=o.SoTien;
		row["KhoanTien_ID"]=o.KhoanTien_ID;
		row["KhoanTienNhapTruong_ID"]=o.KhoanTienNhapTruong_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KhoanTienNhapTruongObject,row);
    }
	public void LoadObject(Int32 KhoanTienNhapTruong_ID){
		KhoanTienNhapTruongEntity oLoad=FindKhoanTienNhapTruong(KhoanTienNhapTruong_ID);
		if(oLoad==null){
			oKhoanTienNhapTruong=this.LoadKhoanTienNhapTruong(KhoanTienNhapTruong_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKhoanTienNhapTruong.IsNullKhoanTienNhapTruong_ID;
			if(!bNotIsNull){
				this.ListKhoanTienNhapTruong.Add(oKhoanTienNhapTruong);
			}
		}
		else{
			oKhoanTienNhapTruong=oLoad;
		}
	}
	#region CachedList KhoanTienNhapTruong
	private static List<KhoanTienNhapTruongEntity> _ListKhoanTienNhapTruong=new List<KhoanTienNhapTruongEntity>();
    public List<KhoanTienNhapTruongEntity>ListKhoanTienNhapTruong{
		get{
			return _ListKhoanTienNhapTruong;
		}
	}
	public static List<KhoanTienNhapTruongEntity>ListKhoanTienNhapTruongStatic{
		get{
			return _ListKhoanTienNhapTruong;
		}
	}
	public KhoanTienNhapTruongEntity FindKhoanTienNhapTruong(Int32 KhoanTienNhapTruong_ID){
		KhoanTienNhapTruongEntity oResult=this.ListKhoanTienNhapTruong.Find(delegate(KhoanTienNhapTruongEntity oFind){
			bool result=true; 
			result&=oFind.KhoanTienNhapTruong_ID.Equals(KhoanTienNhapTruong_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKhoanTienNhapTruong(bool clearCache){
		if(_ListKhoanTienNhapTruong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KhoanTienNhapTruongServiceClient dao = new KhoanTienNhapTruongServiceClient();
		_ListKhoanTienNhapTruong=new List<KhoanTienNhapTruongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KhoanTienNhapTruongEntity LoadKhoanTienNhapTruong(Int32 KhoanTienNhapTruong_ID){			
		KhoanTienNhapTruongEntity o=dao.SelectOne(KhoanTienNhapTruong_ID);		
		return o;
	}
	#endregion
}
}
